package com.atguigu.singleton.type2;

/**
 * 单例模式
 * 饿汉式（静态代码块）
 *
 * @author tielei
 * @date 2021-11-17 22:14
 */
public class SingletonTest02 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance == instance1);
        System.out.println(instance.hashCode() == instance1.hashCode());
    }
}

//饿汉式（静态代码块）

/**
 * 缺点和有点与饿汉式（静态变量一样）
 */
class Singleton {
    private Singleton() {}

    private static final Singleton instance;

    static {
        instance = new Singleton();
    }

    public static Singleton getInstance() {
        return instance;
    }
}
